package com.xlf.mail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class MailProperties {

	public static final String PATH = "./config/mail.properties";
	private static String mailserver;
	private static String username;
	private static String password;
	private static String receive;
	private static String send;
	private static String mailtitle;
	private static String mailcontent;
	private static String isstart;
	private static Properties prop = new Properties();
	static {
		try {
			readProperties();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	private static void readProperties() {
		FileInputStream fin = null;
		try {
			fin = new FileInputStream(new File(PATH));
			prop.load(fin);
			mailserver = prop.getProperty("mailserver").toString().trim();
			username = prop.getProperty("username").toString().trim();
			password = prop.getProperty("password").toString().trim();
			receive = prop.getProperty("receive").toString().trim();
			send = prop.getProperty("send").toString().trim();
			mailtitle = prop.getProperty("mailtitle").toString().trim();
			mailcontent = prop.getProperty("mailcontent").toString().trim();
			isstart = prop.getProperty("isstart").toString().trim();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally {
			try {
				fin.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void saveProperties(SendMail mail) {

		prop.put("mailserver", mail.getSmtpServer());
		prop.put("username", mail.getUsername());
		prop.put("password", mail.getPassword());
		prop.put("receive", mail.getTo());
		prop.put("send", mail.getFrom());
		prop.put("mailtitle", mail.getSubject());
		prop.put("mailcontent", mail.getContent());
		prop.put("isstart", mail.getIsstart());
		FileOutputStream outputFile;
		try {
			setMailserver(mail.getSmtpServer());
			setUsername(mail.getUsername());
			setPassword(mail.getPassword());
			setReceive(mail.getTo());
			setSend(mail.getFrom());
			setMailtitle(mail.getSubject());
			setMailcontent(mail.getContent());
			setIsstart(mail.getIsstart()=="off"?"off":"on");
			outputFile = new FileOutputStream(PATH);
			prop.store(outputFile, null);
			outputFile.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {

		}

	}

	public static String getMailserver() {
		return mailserver;
	}

	public static void setMailserver(String mailserver) {
		MailProperties.mailserver = mailserver;
	}

	public static String getUsername() {
		return username;
	}

	public static void setUsername(String username) {
		MailProperties.username = username;
	}

	public static String getPassword() {
		return password;
	}

	public static void setPassword(String password) {
		MailProperties.password = password;
	}

	public static String getReceive() {
		return receive;
	}

	public static void setReceive(String receive) {
		MailProperties.receive = receive;
	}

	public static String getSend() {
		return send;
	}

	public static String getIsstart() {
		return isstart;
	}

	public static void setIsstart(String isstart) {
		MailProperties.isstart = isstart;
	}

	public static void setSend(String send) {
		MailProperties.send = send;
	}

	public static String getMailtitle() {
		return mailtitle;
	}

	public static void setMailtitle(String mailtitle) {
		MailProperties.mailtitle = mailtitle;
	}

	public static String getMailcontent() {
		return mailcontent;
	}

	public static void setMailcontent(String mailcontent) {
		MailProperties.mailcontent = mailcontent;
	}

	public static Properties getProp() {
		return prop;
	}

	public static void setProp(Properties prop) {
		MailProperties.prop = prop;
	}

}
